package org.telegram.bot.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quartz.util.CronUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.telegram.bot.domain.BotJob;
import org.telegram.bot.service.IBotJobService;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 定时任务Controller
 *
 * @author wyx
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/bot/job")
@Slf4j
public class BotJobController extends BaseController
{
    @Autowired
    private IBotJobService botJobService;

    /**
     * 查询定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('bot:job:list')")
    @GetMapping("/list")
    public TableDataInfo list(BotJob botJob)
    {
        startPage();
        List<BotJob> list = botJobService.selectBotJobList(botJob);
        return getDataTable(list);
    }

    /**
     * 导出定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('bot:job:export')")
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BotJob botJob)
    {
        List<BotJob> list = botJobService.selectBotJobList(botJob);
        ExcelUtil<BotJob> util = new ExcelUtil<BotJob>(BotJob.class);
        util.exportExcel(response, list, "定时任务数据");
    }

    /**
     * 获取定时任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('bot:job:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(botJobService.selectBotJobById(id));
    }

    /**
     * 新增定时任务
     */
    @PreAuthorize("@ss.hasPermi('bot:job:add')")
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BotJob botJob)
    {
        return toAjax(botJobService.insertBotJob(botJob));
    }

    /**
     * 修改定时任务
     */
    @PreAuthorize("@ss.hasPermi('bot:job:edit')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BotJob botJob) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(botJob.getCronExpression()))
        {
            return error("操作失败，Cron表达式不正确");
        }
        if (!CronUtils.isValidRate(botJob.getCronExpression(),300L))
        {
            return error("操作失败，执行频率不能低于5分钟/次");
        }
        return toAjax(botJobService.updateBotJob(botJob));
    }

    /**
     * 删除定时任务
     */
    @PreAuthorize("@ss.hasPermi('bot:job:remove')")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) throws SchedulerException {
        return toAjax(botJobService.deleteBotJobByIds(ids));
    }
}
